using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using sbMembership.Library.Presenters.ViewInterfaces;

namespace sbMembership.Library.Presenters
{
    
/// <summary>
/// Summary description for ListUsersPresenter
/// </summary>
    public class ListUsersPresenter
    {
        private IListUsersView view;
        public enum ActionEnum
        {
             updated
            ,added
            ,deleted
            ,none

        }

        public ListUsersPresenter(IListUsersView view)
        {
            if (view == null) throw new ArgumentNullException("view may not be null");

            this.view = view;
        }

        public EventHandler LaunchAddUserEvent;
        public EventHandler DeleteUserCompleteEvent;
        public EventHandler LaunchAssignRoleToUserEvent;
        public EventHandler LaunchEditUserEvent;

        public void InitViewFor(ActionEnum action)
        {
            DisplayMessageFor(action);

            view.Users = Membership.GetAllUsers();
        }

        public void LaunchAddUser()
        {
            LaunchAddUserEvent(this, null);
        }

        public void DeleteUser(string user)
        {
            Membership.DeleteUser(user, true);
            DeleteUserCompleteEvent(this, null);
        }

        private void DisplayMessageFor(ActionEnum action)
        {
            if (action == ActionEnum.updated)
                view.Message = "The user was successfully updated.";
            else if (action == ActionEnum.added)
                view.Message = "The user was successfully added.";
            else if (action == ActionEnum.deleted)
                view.Message = "The user was successfully deleted.";
            else
                view.Message = "Click a user's ID to edit the user.";
        }

        public void LaunchAssignRoleToUser(string userName)
        {
            AssignRoleEventArgs e = new AssignRoleEventArgs();
            e.UserName = userName;
            LaunchAssignRoleToUserEvent(this, e);
        }

        public void LaunchEditUser(string userName)
        {
            EditUserEventArgs e = new EditUserEventArgs();
            e.UserName = userName;
            LaunchEditUserEvent(this, e);
        }

    }
}
